'HIDEasy
'Copyright (C) 2011 ASFX

'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 2
'of the License, or (at your option) any later version.

'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details.

'You should have received a copy of the GNU General Public License
'along with this program; if not, write to the Free Software
'Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Imports System.Runtime.InteropServices
Imports Microsoft.Win32.SafeHandles
Imports PHIDP_PREPARSED_DATA = System.IntPtr
Imports PHIDP_BUTTON_CAPS = System.IntPtr
Imports PHIDP_VALUE_CAPS = System.IntPtr
Imports PHIDP_DATA = System.IntPtr
Imports USAGE = System.UInt16

<CLSCompliant(False)> _
Friend NotInheritable Class NativeMethods
    <StructLayout(LayoutKind.Sequential)> _
    Structure SECURITY_ATTRIBUTES
        Dim nLength As Integer
        Dim lpSecurityDescriptor As IntPtr
        Dim bInheritHandle As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Friend Structure OVERLAPPED
        Dim Internal As Integer
        Dim InternalHigh As Integer
        Dim Offset As Integer
        Dim OffsetHigh As Integer
        Dim hEvent As IntPtr
    End Structure

    <Flags()> _
    Enum DEVICE_ACCESS As Integer
        ACCESS_NONE = 0
        GENERIC_READ = &H80000000
        GENERIC_WRITE = &H40000000
    End Enum

    '<Flags()> _
    'Enum WAIT_MODE As Integer
    '    WAIT_TIMEOUT = &H102
    '    WAIT_FAILED = &HFFFFFFFF
    '    WAIT_INFINITE = &HFFFF
    'End Enum
    'Friend Const WAIT_OBJECT_0 As Short = 0

    <Flags()> _
    Enum FILE_SHARE_MODE As Short
        None = 0
        FILE_SHARE_DELETE = &H4S
        FILE_SHARE_READ = &H1S
        FILE_SHARE_WRITE = &H2S
    End Enum

    Enum CREATION_DISPOSITION As Integer
        CREATE_ALWAYS = 2
        CREATE_NEW = 1
        OPEN_ALWAYS = 4
        OPEN_EXISTING = 3
        TRUNCATE_EXISTING = 5
    End Enum

    <Flags()> _
    Enum FLAGS_AND_ATTRIBUTES As Integer
        FILE_ATTRIBUTE_ARCHIVE = &H20
        FILE_ATTRIBUTE_ENCRYPTED = &H4000
        FILE_ATTRIBUTE_HIDDEN = &H2
        FILE_ATTRIBUTE_NORMAL = &H80
        FILE_ATTRIBUTE_OFFLINE = &H1000
        FILE_ATTRIBUTE_READONLY = &H1
        FILE_ATTRIBUTE_SYSTEM = &H4
        FILE_ATTRIBUTE_TEMPORARY = &H100
        FILE_FLAG_BACKUP_SEMANTICS = &H2000000
        FILE_FLAG_DELETE_ON_CLOSE = &H4000000
        FILE_FLAG_NO_BUFFERING = &H20000000
        FILE_FLAG_OPEN_NO_RECALL = &H100000
        FILE_FLAG_OPEN_REPARSE_POINT = &H200000
        FILE_FLAG_OVERLAPPED = &H40000000
        FILE_FLAG_POSIX_SEMANTICS = &H100000
        FILE_FLAG_RANDOM_ACCESS = &H10000000
        FILE_FLAG_SEQUENTIAL_SCAN = &H8000000
        FILE_FLAG_WRITE_THROUGH = &H80000000
    End Enum

    <Flags()> _
    Public Enum FLAGS_DIGCF As Integer
        DIGCF_DEFAULT = &H1
        ' only valid with DIGCF_DEVICEINTERFACE
        DIGCF_PRESENT = &H2
        DIGCF_ALLCLASSES = &H4
        DIGCF_PROFILE = &H8
        DIGCF_DEVICEINTERFACE = &H10
    End Enum

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure SP_DEVINFO_DATA
        Dim cbSize As Integer
        Dim ClassGuid As Guid
        Dim DevInst As Integer
        Dim Reserved As IntPtr
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure SP_DEVICE_INTERFACE_DATA
        Public cbSize As Integer
        Public InterfaceClassGuid As Guid
        Public Flags As Integer
        Public Reserved As IntPtr
    End Structure

    Enum FLAGS_SPDRP As Integer
        SPDRP_ADDRESS = &H1C
        SPDRP_BUSNUMBER = &H15
        SPDRP_BUSTYPEGUID = &H13
        SPDRP_CAPABILITIES = &HF
        SPDRP_CHARACTERISTICS = &H1B
        SPDRP_CLASS = 7
        SPDRP_CLASSGUID = 8
        SPDRP_COMPATIBLEIDS = 2
        SPDRP_CONFIGFLAGS = &HA
        SPDRP_DEVICE_POWER_DATA = &H1E
        SPDRP_DEVICEDESC = 0
        SPDRP_DEVTYPE = &H19
        SPDRP_DRIVER = 9
        SPDRP_ENUMERATOR_NAME = &H16
        SPDRP_EXCLUSIVE = &H1A
        SPDRP_FRIENDLYNAME = &HC
        SPDRP_HARDWAREID = 1
        SPDRP_LEGACYBUSTYPE = &H14
        SPDRP_LOCATION_INFORMATION = &HD
        SPDRP_LOWERFILTERS = &H12
        SPDRP_MFG = &HB
        SPDRP_PHYSICAL_DEVICE_OBJECT_NAME = &HE
        SPDRP_REMOVAL_POLICY = &H1F
        SPDRP_REMOVAL_POLICY_HW_DEFAULT = &H20
        SPDRP_REMOVAL_POLICY_OVERRIDE = &H21
        SPDRP_SECURITY = &H17
        SPDRP_SECURITY_SDS = &H18
        SPDRP_SERVICE = 4
        SPDRP_UI_NUMBER = &H10
        SPDRP_UI_NUMBER_DESC_FORMAT = &H1D
        SPDRP_UPPERFILTERS = &H11
    End Enum

#Region "Kernel32"

    <DllImport("kernel32.dll", CharSet := CharSet.Auto, SetLastError := True)> _
    Public Shared Function CreateFile(ByVal lpFileName As String, _
                                      ByVal dwDesiredAccess As DEVICE_ACCESS, _
                                      ByVal dwShareMode As FILE_SHARE_MODE, _
                                      ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
                                      ByVal dwCreationDisposition As CREATION_DISPOSITION, _
                                      ByVal dwFlagsAndAttributes As FLAGS_AND_ATTRIBUTES, _
                                      ByVal hTemplateFile As Integer) As SafeFileHandle
    End Function

    <DllImport("kernel32.dll", SetLastError := True)> _
    Public Shared Function CloseHandle(ByVal hObject As SafeFileHandle) As Boolean
    End Function

#End Region

#Region "setupapi"

    <DllImport("setupapi.dll", CharSet := CharSet.Unicode, SetLastError := True)> _
    Public Shared Function SetupDiGetClassDevs(ByRef ClassGuid As Guid, _
                                               <MarshalAs(UnmanagedType.LPTStr)> ByVal Enumerator As String, _
                                               ByVal hwndParent As SafeFileHandle, _
                                               ByVal Flags As FLAGS_DIGCF) As SafeFileHandle
    End Function

    <DllImport("setupapi.dll", SetLastError := True)> _
    Public Shared Function SetupDiEnumDeviceInfo(ByVal DeviceInfoSet As SafeFileHandle, _
                                                 ByVal MemberIndex As Integer, _
                                                 ByRef DeviceInfoData As SP_DEVINFO_DATA) As Boolean
    End Function

    <DllImport("setupapi.dll", SetLastError := True)> _
    Public Shared Function SetupDiDestroyDeviceInfoList(ByVal DeviceInfoSet As SafeFileHandle) As Integer
    End Function

    <DllImport("setupapi.dll", SetLastError := True)> _
    Public Shared Function SetupDiEnumDeviceInterfaces(ByVal DeviceInfoSet As SafeFileHandle, _
                                                       ByRef DeviceInfoData As SP_DEVINFO_DATA, _
                                                       ByRef InterfaceClassGuid As Guid, _
                                                       ByVal MemberIndex As Integer, _
                                                       ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA) As Boolean
    End Function

    <DllImport("setupapi.dll", CharSet := CharSet.Auto, SetLastError := True)> _
    Public Shared Function SetupDiGetDeviceInterfaceDetail(ByVal DeviceInfoSet As SafeFileHandle, _
                                                           ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA, _
                                                           ByVal DeviceInterfaceDetailData As SafeFileHandle, _
                                                           ByVal DeviceInterfaceDetailDataSize As Integer, _
                                                           ByRef RequiredSize As Integer, _
                                                           ByVal DeviceInfoData As SafeFileHandle) As Boolean
    End Function

    <DllImport("setupapi.dll", EntryPoint := "SetupDiGetDeviceRegistryProperty", SetLastError := True)> _
    Public Shared Function SetupDiGetDeviceRegistryProperty(ByVal DeviceInfoSet As SafeFileHandle, _
                                                            ByRef DeviceInfoData As SP_DEVINFO_DATA, _
                                                            ByVal [Property] As FLAGS_SPDRP, _
                                                            ByRef PropertyRegDataType As SafeFileHandle, _
                                                            ByVal PropertyBuffer As Byte(), _
                                                            ByVal PropertyBufferSize As Integer, _
                                                            ByRef RequiredSize As Integer) As Boolean
    End Function

    <DllImport("setupapi.dll", EntryPoint := "SetupDiGetDeviceRegistryProperty", SetLastError := True)> _
    Public Shared Function SetupDiGetDeviceRegistryProperty(ByVal DeviceInfoSet As SafeFileHandle, _
                                                            ByRef DeviceInfoData As SP_DEVINFO_DATA, _
                                                            ByVal [Property] As FLAGS_SPDRP, _
                                                            ByRef PropertyRegDataType As SafeFileHandle, _
                                                            ByVal PropertyBuffer As IntPtr, _
                                                            ByVal PropertyBufferSize As Integer, _
                                                            ByRef RequiredSize As Integer) As Boolean
    End Function

#End Region

#Region "HID"

    <DllImport("hid.dll")> _
    Public Shared Sub HidD_GetHidGuid(ByRef HidGuid As Guid)
    End Sub

    <DllImport("hid.dll", SetLastError:=True)> _
    Public Shared Function HidD_GetPreparsedData(ByVal HidDeviceObject As SafeFileHandle, _
                                                 ByRef PreparsedData As PHIDP_PREPARSED_DATA) As Boolean
    End Function

    <DllImport("hid.dll", SetLastError:=True)> _
    Public Shared Function HidD_FreePreparsedData(ByVal PreparsedData As PHIDP_PREPARSED_DATA) As Boolean
    End Function

    <DllImport("hid.dll", SetLastError:=True)> _
    Public Shared Function HidD_GetAttributes(ByVal HidDeviceObject As SafeFileHandle, _
                                              ByRef Attributes As HIDClass.HIDD_ATTRIBUTES) As Boolean
    End Function

    <DllImport("hid.dll", SetLastError:=True)> _
    Public Shared Function HidD_GetProductString(ByVal HidDeviceObject As SafeFileHandle, _
                                                 ByVal Buffer As IntPtr, _
                                                 ByVal BufferLength As UInt32) As Boolean
    End Function

    <DllImport("hid.dll", SetLastError:=True)> _
    Public Shared Function HidD_GetManufacturerString(ByVal HidDeviceObject As SafeFileHandle, _
                                                      ByVal Buffer As IntPtr, _
                                                      ByVal BufferLength As UInt32) As Boolean
    End Function

    <DllImport("hid.dll", SetLastError:=True)> _
    Public Shared Function HidD_GetSerialNumberString(ByVal HidDeviceObject As SafeFileHandle, _
                                                      ByVal Buffer As IntPtr, _
                                                      ByVal BufferLength As UInt32) As Boolean
    End Function

    <DllImport("hid.dll", SetLastError:=True)> _
    Public Shared Function HidD_GetIndexedString(ByVal HidDeviceObject As SafeFileHandle, _
                                                 ByVal StringIndex As UInt32, _
                                                 ByVal Buffer As IntPtr, _
                                                 ByVal BufferLength As UInt32) As Boolean
    End Function

    <DllImport("hid.dll", SetLastError:=True)> _
    Public Shared Function HidD_GetInputReport(ByVal HidDeviceObject As SafeFileHandle, _
                                               ByVal ReportBuffer As IntPtr, _
                                               ByVal ReportBufferLength As UInt32) As Boolean
    End Function

    <DllImport("hid.dll", SetLastError:=True)> _
    Public Shared Function HidD_SetOutputReport(ByVal HidDeviceObject As SafeFileHandle, _
                                                ByVal ReportBuffer As IntPtr, _
                                                ByVal ReportBufferLength As UInt32) As Boolean
    End Function

    <DllImport("hid.dll", SetLastError:=True)> _
    Public Shared Function HidD_SetFeature(ByVal HidDeviceObject As SafeFileHandle, _
                                           ByVal ReportBuffer As IntPtr, _
                                           ByVal ReportBufferLength As UInt32) As Boolean
    End Function

    <DllImport("hid.dll")> _
    Public Shared Function HidP_GetCaps(ByVal PreparsedData As PHIDP_PREPARSED_DATA, _
                                        ByRef Capabilities As HIDClass.HIDP_CAPS) As HIDClass.HID_ERROR_CODE
    End Function

    <DllImport("hid.dll")> _
    Public Shared Function HidP_GetLinkCollectionNodes(ByVal lpLinkCollectionNodes As IntPtr, _
                                                       ByRef LinkCollectionNodesLength As ULong, _
                                                       ByVal PreparsedData As PHIDP_PREPARSED_DATA) _
        As HIDClass.HID_ERROR_CODE
    End Function

    <DllImport("hid.dll")> _
    Public Shared Function HidP_GetButtonCaps(ByVal ReportType As HIDClass.HIDP_REPORT_TYPE, _
                                              ByVal ButtonCaps As PHIDP_BUTTON_CAPS, _
                                              ByRef ButtonCapsLength As UInt16, _
                                              ByVal PreparsedData As PHIDP_PREPARSED_DATA) As HIDClass.HID_ERROR_CODE
    End Function

    <DllImport("hid.dll")> _
    Public Shared Function HidP_GetValueCaps(ByVal ReportType As HIDClass.HIDP_REPORT_TYPE, _
                                             ByVal ValueCaps As PHIDP_VALUE_CAPS, _
                                             ByRef ValueCapsLength As UInt16, _
                                             ByVal PreparsedData As PHIDP_PREPARSED_DATA) As HIDClass.HID_ERROR_CODE
    End Function

    <DllImport("hid.dll")> _
    Public Shared Function HidP_MaxDataListLength(ByVal ReportType As HIDClass.HIDP_REPORT_TYPE, _
                                                  ByVal PreparsedData As PHIDP_PREPARSED_DATA) As UInt32
    End Function

    <DllImport("hid.dll")> _
    Public Shared Function HidP_GetData(ByVal ReportType As HIDClass.HIDP_REPORT_TYPE, _
                                        ByVal DataList As PHIDP_DATA, _
                                        ByRef DataLength As UInt32, _
                                        ByVal PreparsedData As PHIDP_PREPARSED_DATA, _
                                        ByVal Report As IntPtr, _
                                        ByVal ReportLength As UInt32) As HIDClass.HID_ERROR_CODE
    End Function

    <DllImport("hid.dll")> _
    Public Shared Function HidP_SetData(ByVal ReportType As HIDClass.HIDP_REPORT_TYPE, _
                                        ByVal DataList As PHIDP_DATA, _
                                        ByRef Datalength As UInt32, _
                                        ByVal PreparsedData As PHIDP_PREPARSED_DATA, _
                                        ByVal Report As IntPtr, _
                                        ByVal ReportLength As UInt32) As HIDClass.HID_ERROR_CODE

    End Function

    '<DllImport("hid.dll")> _
    'Public Shared Function HidP_SetScaledUsageValue(ByVal ReportType As HIDP_REPORT_TYPE, _
    '                                          ByVal UsagePage As USAGE, _
    '                                          ByVal LinkCollection As UShort, _
    '                                          ByVal Usage As USAGE, _
    '                                          ByVal UsageValue As Int32, _
    '                                          ByVal PreparsedData As PHIDP_PREPARSED_DATA, _
    '                                          ByVal Report As Byte(),
    '                                          ByVal ReportLength As UInt32) As HID_ERROR_CODE
    'End Function

    <DllImport("hid.dll")> _
    Public Shared Function HidP_GetExtendedAttributes(ByVal ReportType As HIDClass.HIDP_REPORT_TYPE, _
                                                      ByVal DataIndex As UShort, _
                                                      ByVal PreparsedData As PHIDP_PREPARSED_DATA, _
                                                      ByVal Attributes As IntPtr, _
                                                      ByRef LengthAttributes As UInt32) As HIDClass.HID_ERROR_CODE
    End Function

    <DllImport("hid.dll")> _
    Public Shared Function HidP_GetScaledUsageValue(ByVal ReportType As HIDClass.HIDP_REPORT_TYPE, _
                                                    ByVal UsagePage As USAGE, _
                                                    ByVal LinkCollection As UShort, _
                                                    ByVal Usage As USAGE, _
                                                    ByRef UsageValue As Int32, _
                                                    ByVal PreparsedData As PHIDP_PREPARSED_DATA, _
                                                    ByVal Report As IntPtr, _
                                                    ByVal ReportLength As UInt32) As HIDClass.HID_ERROR_CODE
    End Function

    <DllImport("hid.dll")> _
    Public Shared Function HidP_SetUsageValueArray(ByVal ReportType As HIDClass.HIDP_REPORT_TYPE, _
                                                   ByVal UsagePage As USAGE, _
                                                   ByVal LinkCollection As UInt16, _
                                                   ByVal Usage As USAGE, _
                                                   ByRef UsageValue As Int32, _
                                                   ByVal UsageValueByteLength As UInt16, _
                                                   ByVal PreparsedData As PHIDP_PREPARSED_DATA, _
                                                   ByVal Report As IntPtr, _
                                                   ByVal ReportLength As UInt32) As HIDClass.HID_ERROR_CODE

    End Function

    <DllImport("hid.dll")> _
    Public Shared Function HidP_SetUsageValue(ByVal ReportType As HIDClass.HIDP_REPORT_TYPE, _
                                              ByVal UsagePage As USAGE, _
                                              ByVal LinkCollection As UInt16, _
                                              ByVal Usage As USAGE, _
                                              ByVal UsageValue As UInt32, _
                                              ByVal PreparsedData As PHIDP_PREPARSED_DATA, _
                                              ByVal Report As IntPtr, _
                                              ByVal ReportLength As UInt32) As HIDClass.HID_ERROR_CODE

    End Function

    <DllImport("hid.dll")> _
    Public Shared Function HidP_SetUsages(ByVal ReportType As HIDClass.HIDP_REPORT_TYPE, _
                                          ByVal UsagePage As USAGE, _
                                          ByVal LinkCollection As UInt16, _
                                          ByVal Usages As IntPtr, _
                                          ByRef UsageLength As UInt32, _
                                          ByVal PreparsedData As PHIDP_PREPARSED_DATA, _
                                          ByVal Report As IntPtr, _
                                          ByVal ReportLength As UInt32) As HIDClass.HID_ERROR_CODE

    End Function

    <DllImport("hid.dll")> _
    Public Shared Function HidP_UnsetUsages(ByVal ReportType As HIDClass.HIDP_REPORT_TYPE, _
                                            ByVal UsagePage As USAGE, _
                                            ByVal LinkCollection As UInt16, _
                                            ByVal Usages As IntPtr, _
                                            ByRef UsageLength As UInt32, _
                                            ByVal PreparsedData As PHIDP_PREPARSED_DATA, _
                                            ByVal Report As IntPtr, _
                                            ByVal ReportLength As UInt32) As HIDClass.HID_ERROR_CODE

    End Function

    <DllImport("hid.dll")> _
    Public Shared Function HidP_InitializeReportForID(ByVal ReportType As HIDClass.HIDP_REPORT_TYPE, _
                                                      ByVal ReportID As Byte, _
                                                      ByVal PreparsedData As PHIDP_PREPARSED_DATA, _
                                                      ByVal Report As IntPtr, _
                                                      ByVal ReportLength As UInt32) As HIDClass.HID_ERROR_CODE
    End Function

#End Region
End Class
